package com.example.bankend.controller;


import com.example.bankend.model.BankCard;
import com.example.bankend.service.impl.CardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "银行卡管理")
@RestController
@RequestMapping("/api/cards")
public class CardController {
    @Autowired
    private CardService cardService;

    @ApiOperation(value = "增加银行卡数据")
    @PostMapping("/insert")
    public int insert(@RequestBody BankCard bankcard) {
        System.out.println(bankcard.getIsReportLoss());
        System.out.println(bankcard.getSavingID());

//        bankcard.getIsReportLoss()
        return cardService.insert(bankcard);
    }
    //按照卡号删除
    @ApiOperation(value = "根据银行卡号删除银行卡数据")
    @DeleteMapping("/delete/{cardID}")
    public int deleteByPrimaryKey(@PathVariable String cardID) {
        return cardService.deleteByPrimaryKey(cardID);
    }

    //按照卡号更改
    @ApiOperation(value = "根据银行卡号更新数据")
    @PutMapping("/update/{cardID}")
    public int updateByPrimaryKey(@RequestBody BankCard card) {
        return cardService.updateByPrimaryKey(card);
    }

    //修改密码
    @ApiOperation(value = "根据银行卡号修改密码")
    @PutMapping("/updatepassparam")
    public ResponseEntity<Map<String, Boolean>> updatePassParam(@RequestParam String cardid, @RequestParam String pass) {
        BankCard bankCard = new BankCard();
        bankCard.setCardID(cardid);
        bankCard.setPass(pass);
        int result = cardService.updatePassByCardID(bankCard);
        Map<String, Boolean> response = new HashMap<>();
        if (result > 0) {
            response.put("success", true);
        } else {
            response.put("success", false);
        }
        return ResponseEntity.ok(response);
    }
    //按照卡号查找
    @ApiOperation(value = "根据银行卡号查找数据")
    @GetMapping("/select/{cardID}")
    public BankCard selectByPrimaryKey(@PathVariable String cardID) {
        return cardService.selectByPrimaryKey(cardID);
    }
    //按照卡号  挂失功能
    @ApiOperation(value = "根据银行卡号实现挂失功能")
    @PutMapping("/reportloss/{cardID}")
    public ResponseEntity<String> reportLoss(@PathVariable String cardID) {
        int result = cardService.reportLoss(cardID);
        if (result > 0) {
            return ResponseEntity.ok("挂失成功");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("卡号不存在或挂失失败");
        }
    }

    @ApiOperation(value = "根据银行卡号解挂")
    @PutMapping("/dereportloss/{cardID}")
    public ResponseEntity<String> dereportLoss(@PathVariable String cardID) {
        int result = cardService.dereportLoss(cardID);
        if (result > 0) {
            return ResponseEntity.ok("解挂成功");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("卡号不存在或解挂失败");
        }
    }
    @ApiOperation(value = "拿取所有银行卡数据")
    @GetMapping
    public List<BankCard> selectAll() {
        return cardService.selectAll();
    }

    //根据用户名返回用户下的银行卡
    @ApiOperation(value = "根据用户名返回干用户名下所有银行卡数据")
    @GetMapping("/user")
    public List<BankCard> getCardsByUsername(@RequestParam String username) {
        return cardService.getCardsByUsername(username);
    }

    //返回该用户名下总经济
    @ApiOperation(value = "获取该用户名下总经济")
    @GetMapping("/totalBalance")
    public ResponseEntity<Double> getTotalBalanceByUsername(@RequestParam String username) {
        Double totalBalance = cardService.getTotalBalanceByUsername(username);
        if (totalBalance != null) {
            return ResponseEntity.ok(totalBalance);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    //获取总金额（银行需要的指标）
    @ApiOperation(value = "获取数据库里银行卡总经济")
    @GetMapping("/alltotalBalance")
    public ResponseEntity<Double> getTotalBalance() {
        Double totalBalance = cardService.getTotalBalance();
        if (totalBalance != null) {
            return ResponseEntity.ok(totalBalance);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }
    // 新增查询银行卡总数的接口
    @ApiOperation(value = "获取数据库中所有银行卡的数量")
    @GetMapping("/totalCards")
    public ResponseEntity<Integer> getTotalCards() {
        int totalCards = cardService.getTotalCards();
        return ResponseEntity.ok(totalCards);
    }
}